/**  * 封装Axios  * 处理请求、响应错误信息  */


import { Message } from 'element-ui'
import axios from 'axios' //引用axios  
import Cookie from'js-cookie'
axios.defaults.withCredentials=true
const CryptoJS = require('./aes_util.js')

const service = axios.create({
    baseURL: process.env.BASE_URL,
    // nginx转发到后端Springboot 
    withCredentials: true,
    // send cookies when cross-domain requests 
    timeout: 5000
    // request timeout
}) // request interceptor 
service.interceptors.request.use(config => {
    // do something before request is sent  
    //
   if(process.client){
    let city= Cookie.get('city')
    if(city){
    config.headers['city'] =city
    }
   }

    config.headers['Content-Type'] = 'application/json'
   
    // config.headers['-Token'] = getToken()
    return config
}, error => {
    // do something with request error 

    // for debug  
    return Promise.reject(error)
}) // response interceptor 
service.interceptors.response.use(
  /**  * If you want to get http information such as headers or status  * Please return  response => response  */
  /**  * Determine the request status by custom code  * Here is just an example  * You can also judge the status by HTTP Status Code  */

  response => {
    const res = response.data
    //res is my own data  
    if (res.code === '0000') {
      res.data = JSON.parse(CryptoJS.decrypt(res.data ,res.timestamp))
      return Promise.resolve(res.data)
      
    }
    else{

      return Promise.reject(new Error(res.msg || 'Error'))
    }
  },
  error => {

    return Promise.reject(error)
  }
)
export default service //导出封装后的axios